import { getProductDrawingInfoByPage, addProductDrawingInfo, updateProductDrawingInfo, delProductDrawingInfo, exportProductDrawingInfo } from '@/services/design/productDrawingInfo';
import { PlusOutlined } from '@ant-design/icons';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import type { ActionType, ProColumns } from '@ant-design/pro-table';
import ProTable from '@ant-design/pro-table';
import { Button, Form, Input, message, Modal, Tree } from 'antd';
import { sortTran, filterTran } from '@/utils/requestUtil';
import React, { useEffect, useRef, useState } from 'react';
const ProductDrawingInfo: React.FC<{}> = () => {
const actionRef = useRef<ActionType>();
  const [modalVisit, setModalVisit] = useState(false); // 新建和编辑提示框
  const [confirmLoading, setConfirmLoading] = useState(false); //编辑框的disable
  const [formTitle, setFormTitle] = useState('新建'); //
  const [createOrUpdate, setCreateOrUpdate] = useState(false); //编辑框的disable
  const [form] = Form.useForm();
    const ref = useRef(null);
  const columns: ProColumns[] = [
        {
        title: '序号',
        dataIndex: 'index',
        valueType: 'index',
        width: 80,
        },
        {
          title: '归档标识,0:未归档,1:已归档',
          dataIndex: 'archiveFlag',
        },
        {
          title: '图纸编号',
          dataIndex: 'drawingCode',
        },
        {
          title: '柜体图纸序号',
          dataIndex: 'drawingSequence',
        },
        {
          title: '柜体图纸描述',
          dataIndex: 'drawingSpec',
        },
        {
          title: '柜体图纸链接',
          dataIndex: 'drawingUrl',
        },
        {
          title: '数据隔离',
          dataIndex: 'isolation',
        },
        {
          title: '图纸打印标识:0未打印过;1已打印过',
          dataIndex: 'printFlag',
        },
        {
          title: '产品/柜体编号',
          dataIndex: 'productCode',
        },
        {
          title: '租户id',
          dataIndex: 'tenantId',
        },
        {
        title: '创建日期', //表头显示的名称
        search: false,
        dataIndex: 'createTime',
        },
        {
        title: '创建人', //表头显示的名称
        search: false,
        dataIndex: 'createName',
        },
        {
        title: '操作',
        valueType: 'option',
        key: 'option',
        fixed: 'right',
        render: (text, record, _, action) => [
        <a
                key="editKey"
                onClick={() => {

        setModalVisit(true);
        setCreateOrUpdate(true);
        setFormTitle('编辑');
        form.setFieldsValue({
        archiveFlag: record.archiveFlag,
        drawingCode: record.drawingCode,
        drawingSequence: record.drawingSequence,
        drawingSpec: record.drawingSpec,
        drawingUrl: record.drawingUrl,
        isolation: record.isolation,
        printFlag: record.printFlag,
        productCode: record.productCode,
        tenantId: record.tenantId,
        id: record.id,
        });
        }}
        >
        编辑
        </a>,
        <a
                key="deleteKey"
                onClick={() => {


        Modal.confirm({
        title: '删除',
        content: '确定要删除吗',
        okText: '确认',
        cancelText: '取消',
        onOk: async () => {
        await delProductDrawingInfo({ id: record.id });
        actionRef.current.reload();
        },
        });

        }}
        >
        删除
        </a>,
        ],
        },
  ];
  const handleSubmit = async () => {
        setConfirmLoading(true);
        if (formTitle == '新建') {
          const result = await addProductDrawingInfo(form.getFieldsValue());
        if (result != null) {
          message.success('添加成功');
        }
        } else {
          await updateProductDrawingInfo(form.getFieldsValue());
        }
        setConfirmLoading(false);
        setModalVisit(false);
        actionRef.current.reload();
  };

  return (
  <PageHeaderWrapper>
    <ProTable
        headerTitle="xx查询"
        actionRef={actionRef} //用于触发刷新操作等，看api
        rowKey="id" //表格行 key 的取值，可以是字符串或一个函数
        formRef={ref}
        toolBarRender={(action, { selectedRows }) => [
              <Button
                key="button"
                icon={<PlusOutlined />}
                onClick={() => {
                    setCreateOrUpdate(false);
                    setFormTitle('新建');
                    form.resetFields();
                    setModalVisit(true);
                }}
                type="primary"
                >
                新建
                </Button>,
                <Button
                        key="button"
                        icon={<PlusOutlined />}
                onClick={() => {
                const filters = filterTran(ref.current.getFieldsValue());
                exportProductDrawingInfo({
                    current: 1,
                    pageSize: 5000,
                    filters: filters,
                }).then((res) => {
                const aLink = document.createElement('a');
                document.body.appendChild(aLink);
                aLink.style.display = 'none';
                aLink.href = window.URL.createObjectURL(res);
                aLink.setAttribute('download', `ProductDrawingInfo.xlsx`);
                aLink.click();
                document.body.removeChild(aLink);
                });
                }}
                type="primary"
                >
                导出
                </Button>,
        ]}
        request={async (params = {}, sort, filter) => {

                const sorters = sortTran(sort);
                const filters = filterTran(params);
                const result = await getProductDrawingInfoByPage({ ...params, sorters: sorters, filters: filters });
                return {
                data: result.data.content,
                total: result.data.totalElements,
                success: result.success,
                };

        }}
        columns={columns}
        scroll={{ x: 2000, y: 350 }}
        rowSelection={{}}
    />
    <Modal
            title={formTitle}
            visible={modalVisit}
            onOk={handleSubmit}
            confirmLoading={confirmLoading}
            onCancel={() => setModalVisit(false)}
    >
    <Form
            name="wrap"
            labelCol={{ flex: '110px' }}
            labelAlign="left"
            labelWrap
            form={form}
            wrapperCol={{ flex: 1 }}
            colon={false}
    >
          <Form.Item label="id" name="id" hidden={true}>
            <Input />
          </Form.Item>
          <Form.Item
                  label="归档标识,0:未归档,1:已归档"
                  name="archiveFlag"
                  rules={[{ required: true, message: 'Please input your archiveFlag!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="图纸编号"
                  name="drawingCode"
                  rules={[{ required: true, message: 'Please input your drawingCode!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="柜体图纸序号"
                  name="drawingSequence"
                  rules={[{ required: true, message: 'Please input your drawingSequence!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="柜体图纸描述"
                  name="drawingSpec"
                  rules={[{ required: true, message: 'Please input your drawingSpec!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="柜体图纸链接"
                  name="drawingUrl"
                  rules={[{ required: true, message: 'Please input your drawingUrl!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="数据隔离"
                  name="isolation"
                  rules={[{ required: true, message: 'Please input your isolation!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="图纸打印标识:0未打印过;1已打印过"
                  name="printFlag"
                  rules={[{ required: true, message: 'Please input your printFlag!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="产品/柜体编号"
                  name="productCode"
                  rules={[{ required: true, message: 'Please input your productCode!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
          <Form.Item
                  label="租户id"
                  name="tenantId"
                  rules={[{ required: true, message: 'Please input your tenantId!' }]}
          >
            <Input disabled={createOrUpdate} />
          </Form.Item>
        </Form>
      </Modal>
  </PageHeaderWrapper>
  );
  };
  export default ProductDrawingInfo;
